package com.eds.conusmer;

import com.eds.exception.BusException;
import com.eds.log.LogUtil;
import com.eds.pojo.CodeEnum;
import com.eds.pojo.MyMessage;
import com.eds.service.NotificationService;
import com.pojo.Notification;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class NotificationConsumer {
    // 定义死信交换机
    public static final String DLX_EXCHANGE = "dlx.exchange";
    // 定义死信队列
    public static final String DLX_QUEUE = "dlx.queue";
    // 定义死信路由键
    public static final String DLX_ROUTING_KEY = "dlx.routingKey";
    @Autowired
    private NotificationService notificationService;

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(
                            value = "message",
                            arguments = {
                                    // 设置死信交换机
                                    @Argument(name = "x-dead-letter-exchange", value = DLX_EXCHANGE),
                                    // 设置死信路由键
                                    @Argument(name = "x-dead-letter-routing-key", value = DLX_ROUTING_KEY),
                                    // 设置消息过期时间(毫秒)
                                    @Argument(name = "x-message-ttl", value = "10000", type = "java.lang.Integer")
                            }
                    ),
                    exchange = @Exchange(name = "message.exchange"),
                    key = {"message.routingKey"}
            )
    })
    public void receive(MyMessage<Notification> message) {
        try {
            // 保存通知到数据库
            Boolean aBoolean = notificationService.saveNotification(message.getMessage());
            // 添加成功日志
            LogUtil.info(NotificationConsumer.class, "通知保存成功: {}", message.getMessage());

        } catch (Exception e) {
            // 记录异常日志
            LogUtil.error(NotificationConsumer.class, "处理通知失败: {}, 错误: {}",
                    message, e.getMessage());

            // 如果确实需要将消息发送到死信队列，则重新抛出异常
            throw BusException.error(CodeEnum.MESSAGE_HANDLE_ERROR.getCode(),
                    CodeEnum.MESSAGE_HANDLE_ERROR.getMessage(),
                    message);
        }
    }

    // 死信队列监听器
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(DLX_QUEUE),
                    exchange = @Exchange(name = DLX_EXCHANGE),
                    key = {DLX_ROUTING_KEY}
            )
    })
    public void handleDeadLetter(MyMessage<Notification> message) {
        try {
            // 检查是否已经保存过
            // 可以添加查询逻辑确认通知是否已存在

            // 如果确认通知未保存过，可以尝试重新保存
            // notificationService.saveNotification(message.getMessage());
            // LogUtil.info(NotificationConsumer.class, "死信队列消息处理成功: {}", message);

            // 如果只需要记录日志，不做其他处理
            LogUtil.error(NotificationConsumer.class, "消息处理失败，已进入死信队列: {}", message);
        } catch (Exception e) {
            LogUtil.error(NotificationConsumer.class, "处理死信队列消息失败: {}, 错误: {}",
                    message, e.getMessage());
        }
    }
}